package main

import (
	"context"
	"errors"
	"fmt"
	"github.com/micro/go-micro/v2"
	"helloworld/message"
)

/**
go version:go1.15
go-micro微服务框架:文档：https://www.bookstack.cn/read/go-micro/c1363abef523ce66.md
下载go-micro: go get github.com/micro/go-micro/v2
 */

type StudentManager struct {}

func (h *StudentManager) GetStudent(ctx context.Context, in *message.StudentRequest, out *message.Student) error {
	fmt.Println("接收到client端请求...")
	studentMap := map[string]message.Student{
		"jimmy":message.Student{Name:"jimmy",Classes:"二年一班",Grade:65},
		"ztind":message.Student{Name:"ztind",Classes:"二年二班",Grade:88},
		"jack":message.Student{Name:"jack",Classes:"二年三班",Grade:100},
	}
	student := studentMap[in.Name]
	if student.Name!=""{
		*out = student
		return nil
	}
	return errors.New(in.Name + "not exists")
}

func main(){
	//创建一个服务
	service := micro.NewService(
		micro.Name("student-server"),
		micro.Version("v1.0.0"),
		)
	//初始化服务的option配置
	service.Init()
	//注册服务处理
	message.RegisterSetudenServiceHandler(service.Server(),&StudentManager{})
	//运行服务
	service.Run()
}
